package org.dc.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import org.dc.entity.Visit;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 3/8/12
 * @time: 8:30 PM
 */
public class VisitDaoImpl extends SQLiteOpenHelper implements VisitDao{

    private DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    private DateFormat createdFormat = new SimpleDateFormat("yyyyMMddhhmm");

    public VisitDaoImpl(Context context) {
        super(context, "visit.db", null, 12);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table visit (visitId integer primary key, date integer, time integer, " +
                "created integer, description Text, length integer);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS visit");
        onCreate(db);
    }

    @Override
    public void save(Visit visit) {
        if(visit.getVisitId() > 0){
            ContentValues values = new ContentValues();
            values.put("date", dateFormat.format(visit.getDate()));
            values.put("time", visit.getTime());
            values.put("description", visit.getDescription());
            values.put("length", visit.getLength());
            getWritableDatabase().update("visit", values, "visitid = ?", new String[]{String.valueOf(visit.getVisitId())});
        } else {
            ContentValues values = new ContentValues();
            values.put("date", dateFormat.format(visit.getDate()));
            values.put("time", visit.getTime());
            values.put("description", visit.getDescription());
            values.put("created", createdFormat.format(visit.getCreated()));
            values.put("length", visit.getLength());
            visit.setVisitId((int) getWritableDatabase().insertOrThrow("visit", null, values));
        }
    }

    @Override
    public void delete(Visit visit) {
        if(visit.getVisitId() > 0){
            getWritableDatabase().delete("visit", "visitId = ? ", new String[]{String.valueOf(visit.getVisitId())});
        }
    }

    @Override
    public List<Visit> getVisistsForDay(Date date) {
        List<Visit> ret = new ArrayList<Visit>();
        Cursor cursor = getReadableDatabase().query("visit", new String[]{"visitid, date, time, description, created, length"},
                "date = ? ", new String[]{dateFormat.format(date)}, null, null, "time");
        
        while(cursor.moveToNext()){
            try {
                Visit visit = new Visit();
                visit.setVisitId(cursor.getInt(0));
                visit.setDate(dateFormat.parse(cursor.getString(1)));
                visit.setTime(cursor.getInt(2));
                visit.setDescription(cursor.getString(3));
                visit.setCreated(createdFormat.parse(cursor.getString(4)));
                visit.setLength(cursor.getInt(5));
                ret.add(visit);
            } catch (ParseException e) {
                Log.w("ParseException", cursor.getString(1));
                Log.w("ParseException", cursor.getString(4));
                Log.w("ParseException", e);
            }
        }
        
        return  ret;
    }
}
